﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Xml;
using CommonHelp;
using Core.Unity;
using Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Services;
using ViewModels;

namespace Commands {
    /// <summary>
    /// 卫星云图
    /// </summary>
    public class QueryWXYTCommand : CommandBase {
        //private IXContainer _hook;
        //private IDisplayTable _IDisplayTable;

        //public override void OnCreate(object hook) {
        //    base.OnCreate(hook);
        //    _hook = hook as IXContainer;

        //    _IDisplayTable = _hook.Resolve<IDisplayTable>();
        //}

        //public override void OnClick(object parameter) {
        //    base.OnClick(parameter);
        //    HttpHelper http = new HttpHelper("http://typhoon.weather.com.cn/data/fy2cd_eir_achn_webMercator/FY2E.xml");
        //    http.GetAsync(AfterGetData);

        //    this.IsEnabled = false;
        //}

        //private void AfterGetData(string responseStr) {
        //    try {
        //        //_hook.Resolve<WebViewModel>().Url = "http://typhoon.weather.com.cn/";

        //        XmlDocument doc = new XmlDocument();
        //        doc.LoadXml(responseStr);

        //        List<WXYTInfo> infos = new List<WXYTInfo>();
        //        for (int i = 0; i < doc.ChildNodes.Count; i++) {
        //            var node = doc.ChildNodes[i];
        //            if (node.NodeType == XmlNodeType.XmlDeclaration || node.NodeType == XmlNodeType.Comment) {
        //                continue;
        //            }
        //            foreach (XmlNode item in node.ChildNodes) {
        //                string jsonStr = JsonConvert.SerializeXmlNode(item, Newtonsoft.Json.Formatting.None, true);
        //                jsonStr = jsonStr.Replace("\"@", "\"");
        //                WXYTInfo info = JsonConvert.DeserializeObject<WXYTInfo>(jsonStr);
        //                info.ProType = node.Attributes["proType"].Value;
        //                infos.Add(info);
        //            }
        //        }
        //        _IDisplayTable.DisplayData = infos.ToArray();
        //        _IDisplayTable.AddRowVisitor = new AddWXYTInfo(_hook.Resolve<DbHelp.DbHelper>());
        //        _hook.Resolve<ITextHint>().AddText(DateTime.Now.ToString() + " 获取到 " + infos.Count.ToString() + " 条 卫星云图 数据。");
        //    }
        //    catch (Exception ex) {
        //        CommonHelp.LogHelper.Error(ex);
        //    }
        //    finally {
        //        this.IsEnabled = true;
        //    }
        //}
    }
}
